package com.fitness.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.front.exception.BizException;
import com.fitness.front.mapper.RechargeMapper;
import com.fitness.front.modal.wallet.entity.MemberWallet;
import com.fitness.front.modal.wallet.entity.Recharge;
import com.fitness.front.service.IMemberWalletService;
import com.fitness.front.mapper.MemberWalletMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class MemberWalletServiceImpl extends ServiceImpl<MemberWalletMapper, MemberWallet> implements IMemberWalletService {

    private final RechargeMapper rechargeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean payment(Long memberId, String memberName, Long amount,String comment) {
        QueryWrapper<MemberWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        MemberWallet memberWallet = this.baseMapper.selectOne(queryWrapper);
        if(memberWallet == null || memberWallet.getAmount() < amount){
            log.error("钱包余额小于待支付金额，支付失败");
            throw new BizException("钱包余额小于待支付金额，支付失败");
        }
        memberWallet.setAmount(memberWallet.getAmount() - amount);
        addRechargeRecord("payment",memberId,memberName,amount,comment);

        return this.baseMapper.updateById(memberWallet) > 0;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean refund(Long memberId, String memberName, Long amount,String comment) {
        QueryWrapper<MemberWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        MemberWallet memberWallet = this.baseMapper.selectOne(queryWrapper);
        if(memberWallet == null){
            throw new BizException("没有钱包，退款失败");
        }
        memberWallet.setAmount(memberWallet.getAmount() + amount);
        addRechargeRecord("refund",memberId,memberName,amount,comment);
        return this.baseMapper.updateById(memberWallet) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean recharge(Long memberId, String memberName, Long amount,String comment) {
        QueryWrapper<MemberWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        MemberWallet memberWallet = this.baseMapper.selectOne(queryWrapper);
        if(memberWallet == null){
            memberWallet = new MemberWallet();
            memberWallet.setMemberId(memberId);
            memberWallet.setAmount(amount);
            memberWallet.setMemberName(memberName);
            boolean result = this.baseMapper.insert(memberWallet) > 0;
            addRechargeRecord("recharge",memberId,memberName,amount,comment);
            return result;
        }
        memberWallet.setAmount(memberWallet.getAmount() + amount);
        addRechargeRecord("recharge",memberId,memberName,amount,comment);
        return this.baseMapper.updateById(memberWallet) > 0;
    }

    private void addRechargeRecord(String type,Long memberId,String memberName,Long amount,String comment) {
        Recharge recharge = new Recharge();
        recharge.setMemberId(memberId);
        recharge.setMemberName(memberName);
        recharge.setAmount(amount);
        recharge.setType(type);
        recharge.setRemark(comment);
        rechargeMapper.insert(recharge);
    }
}
